import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';


///  视频播放view
class VideoView extends StatefulWidget {
  final String path;
  final bool autoPlay;
  final bool looping;

  const VideoView({
    super.key,
    required this.path,
    required this.autoPlay,
    required this.looping,
  });

  @override
  State<VideoView> createState() => _VideoViewState();
}

class _VideoViewState extends State<VideoView> {
  late VideoPlayerController _videoPlayerController;

  @override
  void initState() {
    super.initState();

    initController();
  }

  @override
  void dispose() {
    super.dispose();
    _videoPlayerController.dispose();
  }

  initController() {
    _videoPlayerController = VideoPlayerController.asset(widget.path);
    _videoPlayerController.addListener(() {
      //视频播放
    });
    _videoPlayerController.setLooping(widget.looping);
    _videoPlayerController.initialize().then((value) {
      if (mounted) {
        setState(() {});
      }
    });

    if (widget.autoPlay) {
      _videoPlayerController.play();
    }
  }

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: _videoPlayerController.value.aspectRatio,
      child: Container(
        width: MediaQuery.of(context).size.width,
        color: Colors.black,
        child: VideoPlayer(_videoPlayerController),
      ),
    );
  }
}
